﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace SQLToClass
{
    /// <summary>
    /// All database configuration
    /// Created by: Dinh Quoc Vuong
    /// Created date: Oct 11th 2011
    /// </summary>
    [Serializable]
    public class DBConfig
    {
        public int LastDBActiveWorking { get; set; }
        /// <summary>
        /// My sql config
        /// </summary>
        public ConfigBase MySqlConfig { get; set; }
        /// <summary>
        /// Sql config
        /// </summary>
        public SqlConfig SqlConfig { get; set; }

        /// <summary>
        /// Load configuraotion in file to memory
        /// </summary>
        /// <param name="fileName"></param>
        public static DBConfig Load(string fileName = "config.db")
        {
            DBConfig config = null;
            try
            {
                if (File.Exists(fileName))
                {
                    BinaryFormatter binaryFormat = new BinaryFormatter();
                    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        config = (DBConfig)binaryFormat.Deserialize(fs);
                    }
                }
            }
            catch
            {
                //delete old file if load file failed
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
            }
            return config;
        }

        /// <summary>
        /// Save db config into file
        /// </summary>
        /// <param name="config"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool Save(DBConfig config, string filePath = "config.db")
        {
            bool result = false;
            try
            {
                if (config != null)
                {
                    try
                    {
                        using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                        {
                            BinaryFormatter binaryFormat = new BinaryFormatter();
                            binaryFormat.Serialize(fs, config);
                        }
                    }
                    catch
                    {
                        if (File.Exists(filePath))
                        {
                            //delte file when error occur
                            File.Delete(filePath);
                        }
                    }
                }
                else
                {
                    if (File.Exists(filePath))
                    {
                        //simplify. just delete file when config is null
                        File.Delete(filePath);
                        result = true;
                    }
                }
            }
            catch
            {

            }
            return result;
        }
    }
}
